package kbProposal

import (
	"context"

	"mall/service/backend/api/internal/logic/blog/util"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/api/internal/utils"
	"mall/service/backend/api/internal/utils/parse"
	"mall/service/backend/common/i18n"
	"mall/service/blog/rpc/blogclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetProposalListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetProposalListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProposalListLogic {
	return &GetProposalListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetProposalListLogic) GetProposalList(req *types.KBProposalListReq) (resp *types.KBProposalListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	var status uint32
	if req.Status != nil {
		status = *req.Status + 1
	}

	var orderBys []*blogclient.BackendOrderBy
	var filterBys []*blogclient.BackendFilterBy
	if req.Sorter != nil && *req.Sorter != "" {
		orderBys = util.ParseSorter(*req.Sorter)
	}
	if req.Filter != nil && *req.Filter != "" {
		filterBys = util.ParseFilter(*req.Filter)
	}
	var withDeleted uint32
	if adminId := l.ctx.Value("userId").(string); adminId == "1" {
		withDeleted = 1
	}
	data, err := l.svcCtx.BlogRpc.BackendProposalList(l.ctx, &blogclient.BackendProposalListReq{
		Page:           page,
		PageSize:       pageSize,
		AuthorId:       req.AuthorId,
		AuthorNickname: req.AuthorNickname,
		KBEntryId:      req.KBEntryId,
		Status:         &status,
		OrderBy:        orderBys,
		FilterBy:       filterBys,
		WithDeleted:    &withDeleted,
	})
	if err != nil {
		return nil, err
	}

	resp = &types.KBProposalListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.KBProposalListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}

	for _, v := range data.List {
		option := utils.GetAuditOptionByStatus(v.Status)
		var entry *types.KBEntryInfo
		if v.KBEntry != nil {
			entry = &types.KBEntryInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id: v.KBEntry.Id,
				},
				Title:   v.KBEntry.Title,
				Slug:    v.KBEntry.Slug,
				Excerpt: v.KBEntry.Excerpt,
				Status:  v.KBEntry.Status,
			}
		}
		resp.Data.List = append(resp.Data.List, &types.KBProposalInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			AuthorId:       v.AuthorId,
			AuthorNickname: v.AuthorNickname,
			AuthorAvatar:   v.AuthorAvatar,
			AuthorLevel:    v.AuthorLevel,
			KBEntryId:      v.KBEntryId,
			Content:        v.Content,
			Perfumers:      v.Perfumers,
			MainAccords:    v.MainAccords,
			TopNotes:       v.TopNotes,
			MiddleNotes:    v.MiddleNotes,
			BaseNotes:      v.BaseNotes,
			Year:           v.Year,
			Status:         v.Status,
			KBEntry:        entry,
		})
	}

	return resp, nil
}
